$(document).ready(function () {
    $('table').find('tbody').each(function () {
        $(this).on('click', 'tr', function () {
            $(this).parent().find('tr').each(function () {
                if ($(this).hasClass('selected')) {
                    $(this).removeClass('selected');
                }
            });
            $(this).addClass('selected');
        })
    });


    $('#errorMsg').attr('style','display: none');
});

/**
 * 获取table表格的数据项.
 */
function getTableData(tableId) {
    var data = "[";                                                                   //定义数据变量
    $("#" + tableId).find("tbody > tr").each(function () {
        data += "{"
        $(this).find("input, select").each(function () {
            data += "\"" + $(this).attr("name") + "\":\"" + $(this).val() + "\",";
        });
        data += "},"
    });
    
    if (data.length != 1) {                                                           //如果取出了数据，删除多余的符号
        data = data.substring(0, data.length - 1);                                    //删除多余的符号','
    }
    data += "]";                                                                      //添加结束符
    data = eval("(" + data + ")");                                                    //将数据转换成json对象
    return data;                                                                      //返回数据
}

$.fn.serializeObject = function () {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function () {
        if (o[this.name]) {
            if (!o[this.name].push) {
                o[this.name] = [ o[this.name] ];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};

function openModal(url, title, message) {
    $('#modalLabel').text(title);
    $('#modalBody').text(message);
    $('#modalMessage').modal('show');
    $('#modalButton').click(function () {
        window.location.replace(url);
    });
}

(function ($) {
    $.fn.selectbox = function () {

        //用变量idm存储select的id或name
        var idm = $(this).attr("id") || $(this).attr("name");
        if ($("#" + idm + "div").length <= 0) { //判断动态创建的div是否已经存在，如果不存在则创建
            var divHtml = "<div style='display:none' id='" + idm + "div'><input type='text' id='" + idm + "inputText'/></div>";
            $(this).attr("tabindex", -1).after(divHtml);
            $("#" + idm + "div").css({position: "absolute", top: $(this).position().top - 1, left: $(this).position().left - 7}).show();
            $("#" + idm + "inputText").val($(this).val()).css({width: $(this).width() - 13, height: $(this).height() + 1});
            //select注册onchange事件
            $(this).change(function () {
                $("#" + idm + "inputText").val($(this).val());
            })
        }
        ("#" + idm + "inputText").attr("disabled", $(this).attr("disabled"));
        //解决ie6下select浮在div上面的bug
        //$("#" + idm + "div").bgIframe();
        return $("#" + idm + "inputText").val();
    }
})(jQuery);

function operationStatusShow(message){
	scroll(0, 0);
	$("#tips").remove();
	$("#saveStatus").append(message);
	
}

function operationSuccess(){
	var msg = "<div class='alert alert-info' role='alert' id='tips'>" + 
			      "<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×</button>" +
				  "<strong>成功！</strong>" +
			  "</div>";
	operationStatusShow(msg);
}

function operationSuccessWithMessage(message){
	var msg = "<div class='alert alert-info' role='alert' id='tips'>" + 
			      "<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×</button>" +
				  "<strong>" + message + "</strong>" +
			  "</div>";
	operationStatusShow(msg);
}

function operationFailed(){
	var msg = "<div class='alert alert-danger' role='alert' id='tips'>" + 
                  "<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×</button>" +
	              "<strong>失败！</strong>" +
              "</div>";
	operationStatusShow(msg);
}

function operationFailedWithMessage(message){
	var msg = "<div class='alert alert-danger' role='alert' id='tips'>" + 
			      "<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×</button>" +
			      "<strong>失败！原因：</strong>" + message + 
			  "</div>";
	operationStatusShow(msg);
}